package com.fishsticks.model;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.EditText;

import com.fishsticks.R;

public class NewCategoryDialog extends AlertDialog.Builder {
	
	private EditText newCategoryField;
	private DatabaseManager db;
	private AlertDialog errorDialog;
	private String errorString;

	public NewCategoryDialog(Context context, int theme) {
		super(context, theme);
		
		View layout = View.inflate(this.getContext(), R.layout.categories_dialog_box, null);
		newCategoryField = (EditText) layout.findViewById(R.id.new_category_field);
		errorDialog = new AlertDialog.Builder(this.getContext(), AlertDialog.THEME_HOLO_LIGHT).create();
		errorString = this.getContext().getResources().getString(R.string.add_category_error_message_blank_fields);
		db = new DatabaseManager(this.getContext());
		
		

		this.setPositiveButton("Add", new Dialog.OnClickListener() {
	        public void onClick(DialogInterface dialog, int which) {
	           addNewCategory();
	        }
	    });
		
		this.setNegativeButton("Cancel", new Dialog.OnClickListener() {
	        public void onClick(DialogInterface dialog, int which) {
	           dialog.dismiss();
	        }
	    });
		
		this.setView(layout);
	}
	
	public void addNewCategory() {
		int errorCode = db.addCategory(newCategoryField.getText().toString().trim());
        
        if(errorCode == R.string.add_category_error_code_blank_fields) {
     	   	errorDialog.setTitle("Blank Fields");
     	   	errorDialog.setMessage(errorString);
     	   	errorDialog.setButton("Okay", new android.content.DialogInterface.OnClickListener()
			    	{
			    		//Hides dialog after user clicks "ok" button
						@Override
						public void onClick(DialogInterface dialog, int which) {
							errorDialog.dismiss();
						}

			    	});
     	   	errorDialog.show();
        }
        db.close();
	}
}
